package q0141;

public class Solution {
    public boolean hasCycle(ListNode head) {

        ListNode fasterHead = head;

        while (head != null && fasterHead != null) {

            if (fasterHead.next == null) {
                return false;
            }
            fasterHead = fasterHead.next.next;
            if (head == fasterHead) {
                return true;
            }
            head = head.next;
        }
        return false;
    }

    public static void main(String[] args) {
        ListNode n1 = new ListNode(3);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(0);
        ListNode n4 = new ListNode(-2);

//        n1.next = n2;
//        n2.next = n1;
//        n3.next = n4;
//        n4.next = n2;

        System.out.println(new Solution().hasCycle(n1));

    }

}

